package medicine.service;

import org.springframework.stereotype.Service;
import medicine.dao.entity.MedicineRecordPO;
import medicine.dao.entity.MedicineInfoPO;
import medicine.dao.mapper.MedicineMapper;
import util.ErrorCode;
import util.ResponseUtil;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by qianyang on 2018/5/2.
 */
@Service
public class MedicineService {
    @Resource
    private MedicineMapper medicineMapper;

    public String selectMedicineRecord(Integer userId,Date date){
        System.out.println("in selcetMedicineRecord");
        List<MedicineRecordPO> medicineRecordPOS=medicineMapper.selcetMedicineRecord(userId,date);
        System.out.println(medicineRecordPOS);
        List<MedicineRecordVO> medicineRecordVOS=new ArrayList<MedicineRecordVO>();
        for(MedicineRecordPO medicineRecordPO: medicineRecordPOS){
            MedicineRecordVO tmpMedicineRecordVO=new MedicineRecordVO();
            MedicineInfoPO tmpMedicineInfoPO=medicineMapper.getMedicineInfoById(medicineRecordPO.getMedicineId());
            tmpMedicineRecordVO.setMedicineInfoPO(tmpMedicineInfoPO);
            tmpMedicineRecordVO.setMedicineRecordPO(medicineRecordPO);
            medicineRecordVOS.add(tmpMedicineRecordVO);
        }
        if (!medicineRecordPOS.isEmpty()) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", medicineRecordVOS);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "药物记录为空", medicineRecordVOS);
        }
    }

    public String selcetALLMedicine(){
        List<MedicineInfoPO> MedicineInfoPOS=medicineMapper.selcetALLMedicineInfo();
        if (!MedicineInfoPOS.isEmpty()) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", MedicineInfoPOS);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "药物列表为空", MedicineInfoPOS);
        }
    }

    public String selcetMedicineById(Integer medicineId){
        MedicineInfoPO medicineInfoPO=medicineMapper.getMedicineInfoById(medicineId);
        if (medicineInfoPO!=null) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", medicineInfoPO);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "获取药物信息失败", medicineInfoPO);
        }
    }

    public String selcetMedicineRecordById(Integer medicineRecordId){
        MedicineRecordPO medicineRecordPO=medicineMapper.getMedicineRecordById(medicineRecordId);
        if (medicineRecordPO!=null) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", medicineRecordPO);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "获取药物记录失败", medicineRecordPO);
        }
    }

    public String searchMedicineByNmae(String name){
        System.out.println("searchMedicineByNmae");
        List<MedicineInfoPO> MedicineInfoPOS=medicineMapper.selectMedicineInfoByName(name.trim());
        if (!MedicineInfoPOS.isEmpty()) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", MedicineInfoPOS);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "药物列表为空", MedicineInfoPOS);
        }
    }

    public String insertMedicineRecord(MedicineRecordVO medicineRecordVO){
        System.out.println("in insertMedicineRecord");
        MedicineInfoPO tmpMedicineInfo=medicineRecordVO.getMedicineInfoPO();
        MedicineRecordPO tmpMedicineRecord=medicineRecordVO.getMedicineRecordPO();
//        int userid=tmpMedicineRecord.getUserId();
//        Date date=tmpMedicineRecord.getDate();
//        List<MedicineRecordPO> medicineRecordPOS=medicineMapper.selcetMedicineRecord(userid,date);
//        for(MedicineRecordPO medicineRecordPO:medicineRecordPOS){
//            if(medicineRecordPO.getMedicineId()==tmpMedicineRecord.getMedicineId()){
//                System.out.println("insert ,有重复记录");
//                MedicineRecordVO tmpMedicineRecordVO=new MedicineRecordVO();
//                medicineRecordPO.setAmount(medicineRecordPO.getAmount()+tmpMedicineRecord.getAmount());
//                medicineRecordPO.setEnergy(medicineRecordPO.getAmount()*tmpMedicineInfo.getKcal());
//                tmpMedicineRecordVO.setMedicineRecordPO(medicineRecordPO);
//                tmpMedicineRecordVO.setMedicineInfoPO(tmpMedicineInfo);
//                System.out.println(tmpMedicineRecordVO);
//                updateMedicineRecord(tmpMedicineRecordVO);
//                return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "药物记录添加成功(有重复记录)", tmpMedicineRecordVO.getMedicineRecordPO());
//            }
//        }
        System.out.println(tmpMedicineRecord.getDateTime());
        medicineMapper.insertMedicineRecord(tmpMedicineRecord);
        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "药物记录添加成功", tmpMedicineRecord);
    }

    public String delectMedicineRecord(Integer id){
        medicineMapper.delectMedicineRecord(id);
        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "删除成功", "");
    }

    public String updateMedicineRecord(MedicineRecordVO medicineRecordVO){
        int id=medicineRecordVO.getMedicineRecordPO().getId();
        System.out.println("in update: id="+id);
        System.out.println(medicineRecordVO);
        delectMedicineRecord(id);
        insertMedicineRecord(medicineRecordVO);

        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "更新运动药物成功", "");
    }

}